"""
Case Type   : 系统内部使用工具
Case Name   : 执行初始化数据库命令gs_ctl init：指定参数--lc-monetary，--lc-numeric，--lc-time
Create At   : 2022-07-28
Owner       : liu-tong-8848
Description :
    1.执行gs_ctl init的命令，指定参数--lc-monetary
    2.查看初始化生成的目录
    3.执行gs_ctl init的命令，指定参数--lc-numeric
    4.查看初始化生成的目录
    5.执行gs_ctl init的命令，指定参数--lc-time
    6.查看初始化生成的目录
    7.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
    6.成功
    7.成功
History     :
"""

import unittest
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro
import os


class Tools(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.userNode = Node('PrimaryDbUser')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.dir_path1 = os.path.join(macro.DB_INSTANCE_PATH, 'datadir1')
        self.dir_path2 = os.path.join(macro.DB_INSTANCE_PATH, 'datadir2')
        self.dir_path3 = os.path.join(macro.DB_INSTANCE_PATH, 'datadir3')
        self.nodename = 'nodename_132'

    def test_systools(self):
        self.logger.info(f'-----{os.path.basename(__file__)}' + ' start-----')

        text = '--step1:执行gs_ctl init的命令，指定参数--lc-monetary；expect：成功--'
        self.logger.info(text)
        excute_cmd1 = f'source {self.DB_ENV_PATH};' \
                      f'gs_ctl init -D {self.dir_path1} -o ' \
                      f'"--nodename="{self.nodename}" -w ' \
                      f'"{self.userNode.ssh_password}" --lc-monetary=zh_CN.utf8"'
        self.logger.info(excute_cmd1)
        msg1 = self.userNode.sh(excute_cmd1).result()
        self.logger.info(msg1)
        self.assertIn('Success. You can now start the database server of '
                      'single node', msg1, text + '执行失败')

        text = '--step2:查看初始化生成的目录；expect：成功--'
        self.logger.info(text)
        excute_cmd2 = f'ls {self.dir_path1}'
        self.logger.info(excute_cmd2)
        msg2 = self.userNode.sh(excute_cmd2).result()
        self.logger.info(msg2)
        self.assertIn('postgresql.conf' and 'pg_hba.conf', msg2, text + '执行失败')

        text = '--step3:执行gs_ctl init的命令，指定参数--lc-numeric；expect：成功--'
        self.logger.info(text)
        excute_cmd3 = f'source {self.DB_ENV_PATH};' \
                      f'gs_ctl init -D {self.dir_path2} -o ' \
                      f'"--nodename={self.nodename} -w ' \
                      f'"{self.userNode.ssh_password}" --lc-numeric=zh_CN.utf8"'
        self.logger.info(excute_cmd3)
        msg3 = self.userNode.sh(excute_cmd3).result()
        self.logger.info(msg3)
        self.assertIn('Success. You can now start the database server of '
                      'single node', msg3, text + '执行失败')

        text = '--step4:查看初始化生成的目录；expect：成功--'
        self.logger.info(text)
        excute_cmd4 = f'ls {self.dir_path2}'
        self.logger.info(excute_cmd4)
        msg4 = self.userNode.sh(excute_cmd4).result()
        self.logger.info(msg4)
        self.assertIn('postgresql.conf' and 'pg_hba.conf', msg4, text + '执行失败')

        text = '--step5:执行gs_ctl init的命令，指定参数--lc-time；expect：成功--'
        self.logger.info(text)
        excute_cmd5 = f'source {self.DB_ENV_PATH};' \
                      f'gs_ctl init -D {self.dir_path3} -o ' \
                      f'"--nodename={self.nodename} -w ' \
                      f'"{self.userNode.ssh_password}" --lc-time=zh_CN.utf8"'
        self.logger.info(excute_cmd5)
        msg5 = self.userNode.sh(excute_cmd5).result()
        self.logger.info(msg5)
        self.assertIn('Success. You can now start the database server of '
                      'single node', msg5, text + '执行失败')

        text = '--step6:查看初始化生成的目录；expect：成功--'
        self.logger.info(text)
        excute_cmd6 = f'ls {self.dir_path3}'
        self.logger.info(excute_cmd6)
        msg6 = self.userNode.sh(excute_cmd6).result()
        self.logger.info(msg6)
        self.assertIn('postgresql.conf' and 'pg_hba.conf', msg6, text + '执行失败')

    def tearDown(self):
        text = '--step7:清理环境；expect：成功--'
        self.logger.info(text)
        excute_cmd7 = f'rm -rf {self.dir_path1};ls {self.dir_path1}'
        self.logger.info(excute_cmd7)
        msg7 = self.userNode.sh(excute_cmd7).result()
        self.logger.info(msg7)
        self.assertTrue(msg7.find('No such file or directory') > -1)
        excute_cmd8 = f'rm -rf {self.dir_path2};ls {self.dir_path2}'
        self.logger.info(excute_cmd8)
        msg8 = self.userNode.sh(excute_cmd8).result()
        self.logger.info(msg8)
        self.assertTrue(msg8.find('No such file or directory') > -1)
        excute_cmd9 = f'rm -rf {self.dir_path3};ls {self.dir_path3}'
        self.logger.info(excute_cmd9)
        msg9 = self.userNode.sh(excute_cmd9).result()
        self.logger.info(msg9)
        self.assertTrue(msg9.find('No such file or directory') > -1)
        self.logger.info(
            f'-----{os.path.basename(__file__)}' + ' end-----')


